//


#import "MB_ChooseView.h"
#import "MB_ChooseCollectionViewCell.h"

//
@interface MB_ChooseView ()<UICollectionViewDelegate, UICollectionViewDataSource>

/** view*/
@property (nonatomic, strong) UIView *whiteView;
/** */
@property (nonatomic, strong) UICollectionView *collectionView;
/** */
@property (nonatomic, strong) NSMutableArray *imgArray;
/** biaoti*/
@property (nonatomic, strong) UILabel *titleLabel;
/** guanbi*/
@property (nonatomic, strong) UIButton *deleBtn;

@end

@implementation MB_ChooseView

- (instancetype)init {
    if ([super init]) {
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.6];
        self.frame = CGRectMake(0, kMainScreenHeight, kMainScreenWidth, kMainScreenHeight);
        [self setView];
    }
    return self;
}

- (void)setView {
    
    [self addSubview:self.whiteView];
    [self.whiteView addSubview:self.titleLabel];
    [self.whiteView addSubview:self.collectionView];
    [self.whiteView addSubview:self.deleBtn];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MB_ChooseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([MB_ChooseCollectionViewCell class]) forIndexPath:indexPath];
    cell.IMG.image = _imgArray[indexPath.row];
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    if (self.sendImgBlock) {
        self.sendImgBlock(_imgArray[indexPath.row]);
    }
}


#pragma mark ----------------- 懒加载 ----------------
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 10;
        layout.minimumInteritemSpacing = (10);
        layout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kRHeight(70), kMainScreenWidth, kRHeight(170)) collectionViewLayout:layout];
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:[MB_ChooseCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MB_ChooseCollectionViewCell class])];
        layout.itemSize = CGSizeMake((kMainScreenWidth-kRWidth(40))/3, kRHeight(150));
    }
    return _collectionView;
}
- (NSMutableArray *)imgArray {
    if (!_imgArray) {
        _imgArray = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"jdkds"],[UIImage imageNamed:@"jhsdkjr"],[UIImage imageNamed:@"timg-2"],[UIImage imageNamed:@"timg-2i"],[UIImage imageNamed:@"timg"],[UIImage imageNamed:@"view"], nil];
    }
    return _imgArray;
}
//
- (UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc]initWithFrame:CGRectMake(0, kMainScreenHeight, kMainScreenWidth, 0)];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }
    return _whiteView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kRWidth(5), kRHeight(20), kMainScreenWidth-kRWidth(10), kRHeight(50))];
        _titleLabel.textColor = UIColorFromHex(0x8470FF);
        _titleLabel.font = FontPFRegular(kRWidth(17));
        _titleLabel.numberOfLines = 0;
        _titleLabel.text = @"You can set a beautiful background picture for your current mood.";
    }
    return _titleLabel;
}
- (UIButton *)deleBtn {
    if (!_deleBtn) {
        _deleBtn = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMidX(self.whiteView.frame)-kRWidth(35), CGRectGetMaxY(self.collectionView.frame), kRWidth(70), kRHeight(30))];
        [_deleBtn setTitle:@"Close" forState:UIControlStateNormal];
        [_deleBtn setTitleColor:UIColorFromHex(0x8470FF) forState:UIControlStateNormal];
        [_deleBtn addTarget:self action:@selector(remove) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleBtn;
}

//MARK:show
- (void)show {
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.frame = CGRectMake(0, 0, kMainScreenWidth, kMainScreenHeight);
        self.whiteView.frame = CGRectMake(0, CGRectGetMaxY(self.frame)-kRHeight(300), kMainScreenWidth, kRHeight(300));
    } completion:^(BOOL finished) {
    }];
}
- (void)remove{
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.frame = CGRectMake(0, kMainScreenHeight, kMainScreenWidth, kMainScreenHeight);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self remove];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
